package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetMealDishMapper setMealDishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetMeal(SetmealDTO setmealDTO) {

        /**
         * 插入一条
         */
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);

        /**
         * 插入多条
         */
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setMealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuerySetMeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        int page = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();
        PageHelper.startPage(page, pageSize);

        Setmeal setMeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setMeal);
//        setMeal.setCategoryId(Long.valueOf(setmealPageQueryDTO.getCategoryId()));
//        setMeal.setStatus(setmealPageQueryDTO.getStatus());

        Page<SetmealVO> s = setmealMapper.pageQuery(setMeal);

        PageResult pageResult = new PageResult(s.getTotal(), s.getResult());
        return pageResult;
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteSetMeal(List<Integer> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException("不允许删除正在出售的套餐");
            }
        });

        /**
         * 删除n条
         */
        setMealDishMapper.deleteBatchBySetMealId(ids);
        /**
         * 删除1条套餐
         */
        setmealMapper.deleteBatch(ids);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetMeal(Integer id) {

        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.findById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);

        List<SetmealDish> setmealDishList = setMealDishMapper.findBySetMealId(setmeal.getId());
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }


    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void modifySetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal2 = setmealMapper.findById(Math.toIntExact(setmealDTO.getId()));
        if (setmeal2.getStatus() == StatusConstant.ENABLE) {
            throw new RuntimeException("不允许修改正在出售的套餐");
        }


        /**
         * 修改
         */
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        /**
         * 删除
         */
        Long id = setmeal.getId();
        List<Integer> ids = List.of(Math.toIntExact(id));
        setMealDishMapper.deleteBatchBySetMealId(ids);


        /**
         * 插入
         */
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
        setMealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐起售、停售
     * @return
     */
    @Override
    public void modifySetMealStatus(Integer status, Integer id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(Long.valueOf(id));

        setmealMapper.update(setmeal);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
